/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.boot;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Stream;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import com.hyts.infodb.boot.base.BootServerApplication;
import com.hyts.infodb.platform.PlatformApplication;

import lombok.extern.slf4j.Slf4j;

/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
//@EntityScan("com.hyts.infodb")
@Slf4j
//@ComponentScan(basePackages= {"com.hyts.infodb.user"})
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
public class BootApplication {
     
    /**
     * <b>infoBD服务启动服务操作-总入口</b>
     * <p>读取serviceLoader api实现热插拔方式启动服务</p>
     * @param args
     * @exception
     */
    public static void main(String[] args) {
        System.setProperty("spring.devtools.restart.enabled","false");
        log.info("开始启动加载boot本身（{}）启动服务上下文功能！",BootApplication.class.getSimpleName());
        ServiceLoader<BootServerApplication> serviceBootList = ServiceLoader.load(com.hyts.infodb.boot.base.BootServerApplication.class, BootApplication.class.getClassLoader());
        ConfigurableApplicationContext parentApplicationContext = 
                new SpringApplicationBuilder(BootApplication.class)/*.bannerMode(Banner.Mode.OFF)*/.
                web(WebApplicationType.NONE).run(args);
        List<BootServerApplication> serviceList = new LinkedList<>();
        //如果进行排序的话
        for(BootServerApplication service:serviceBootList) {
            serviceList.add(service);
        }
        Collections.sort(serviceList, new Comparator<BootServerApplication>() {
            //升序
            @Override
            public int compare(BootServerApplication o1, BootServerApplication o2) {
                return o1.order() - o2.order();
            }
        });
        //排序方法结束
        for(BootServerApplication service:serviceBootList) {
            service.boot(args, parentApplicationContext);
        }
        log.info("完成启动加载boot本身（{}）启动服务上下文功能！",BootApplication.class.getSimpleName());
    }
}
